package com.aivin.myrecyclerviewsample.activity;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Bundle;
import android.os.MessageQueue;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.myrecyclerviewsample.R;
import com.aivin.myrecyclerviewsample.custom.MyAdapter;
import com.aivin.myrecyclerviewsample.custom.MyCustomItemAnimator;
import com.aivin.myrecyclerviewsample.custom.MyMarginDecoration;
import com.aivin.myrecyclerviewsample.custom.StackLayoutManager;
import com.aivin.myrecyclerviewsample.tool.WkLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,   WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        initView(this);
        initData(this);
    }


    private RecyclerView recyclerView ;
    private StackLayoutManager stackLayoutManager ;
    private void initView(Context mContext){
        recyclerView = findViewById( R.id.recyclerView);
        recyclerView.setHasFixedSize(true);


        stackLayoutManager = new StackLayoutManager( mContext ,-30);
        recyclerView.setLayoutManager(stackLayoutManager);

        RecyclerView.ItemDecoration itemDecoration = new MyMarginDecoration(mContext );
        //recyclerView.addItemDecoration(itemDecoration);

        //DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
        DefaultItemAnimator defaultItemAnimator = new MyCustomItemAnimator();
        defaultItemAnimator.setRemoveDuration(1000);
        defaultItemAnimator.setAddDuration(200);
        defaultItemAnimator.setMoveDuration(200);
        defaultItemAnimator.setChangeDuration(2000);
        recyclerView.setItemAnimator(defaultItemAnimator);

        findViewById(R.id.addBtn).setOnClickListener(onClickListener);
        findViewById(R.id.subBtn).setOnClickListener(onClickListener);
        findViewById(R.id.swapBtn).setOnClickListener(onClickListener);
        findViewById(R.id.updateBtn).setOnClickListener(onClickListener);
    }


    private MyAdapter myAdapter ;
    private void initData(Context mContext){
        List<String> datas = new ArrayList<>() ;
        for(int i=1 ;i<=20 ;i++){
            datas.add(String.valueOf(i)) ;
        }
        myAdapter = new MyAdapter(datas ,onAdapterItemClickListener);
        recyclerView.setAdapter(myAdapter);
    }


    private MyAdapter.OnAdapterItemClickListener onAdapterItemClickListener =
            new MyAdapter.OnAdapterItemClickListener() {
        @Override
        public void onItemClick(int itemIndex) {
            int currentFocusIndex = stackLayoutManager.findShouldSelectPosition() ;
            WkLog.showMsg("onItemClick="+ itemIndex  + "  "+ currentFocusIndex);
            if(itemIndex ==currentFocusIndex){
                return;
            }
            stackLayoutManager.smoothScrollToPosition(itemIndex, new StackLayoutManager.OnStackListener() {
                @Override
                public void onFocusAnimEnd() {
                    WkLog.showMsg("动画结束，可以跳转等...");
                }
            });
        }
    };


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(view.getId()==R.id.addBtn){
                int size = myAdapter.getItemCount() +1 ;
                myAdapter.addOneItem(0 ,">>> "+size);
            }

            else if(view.getId()==R.id.subBtn){
                myAdapter.removeOnItem(0);
            }

            else if(view.getId()==R.id.swapBtn){
                myAdapter.itemSwap(4, 0);
            }

            else if(view.getId()==R.id.updateBtn){

                myAdapter.updateItem(1, getRadomInt2(1,10)+"");
            }


        }
    };

    public int getRadomInt2(int startNum, int endNum){
        return new Random().nextInt(endNum - startNum + 1) + startNum;
    }


    private void test( View view){

        view.setRotationY(20);
        view.setRotationX(20);
    }


    public void startAnimator(View view , MessageQueue.IdleHandler idleHandler){

        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor", 0XFFF8080, 0xFF8080FF);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, "backgroundColor", 0XFFF8080, 0xFF8080FF);
        objectAnimator.setEvaluator(new ArgbEvaluator());//设置估值器

        objectAnimator.setInterpolator(new AccelerateInterpolator()); // 估值器1
        objectAnimator.setInterpolator(new DecelerateInterpolator()); // 估值器2

        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                //  ---





            }
        });

        objectAnimator.start();

    }


    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }
}
